﻿using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.Json;
using Amiable.SDK.Enum;
using Amiable.SDK.EventArgs;
using Amiable.SDK.Interface;
using AmiCheckin.Code.Util;

namespace AmiCheckin.Code.BotEvents
{
    public class MasterCommand:IPluginEvent
    {
        private AmiableMessageEventArgs _amiable;
        public void Process(AmiableEventArgs _e)
        {
            var e = _e as AmiableMessageEventArgs;
            _amiable = e;
            if (e.RawMessage.StartsWith(".amic"))
            {
                var config = ConfigUtil.GetConfig();
                var args = e.RawMessage.Substring(5).Trim().Split(' ');
                var newargs = args.Skip(1).ToArray(); //去掉前面-x的args
                switch (args.ElementAtOrDefault(0))
                {
                    case "reload" :
                        if (config.IsMaster(e.UserId))
                            ReloadCommand(newargs);
                        break;
                    case "cfg" :
                        if (config.IsMaster(e.UserId))
                            ConfigCommand(newargs);
                        break;
                }
            } 
        }

        private void ConfigCommand(string[] newargs)
        {
            var sb = new StringBuilder();
            foreach (var set in AmiCheckin.Config.CheckinSets)
            {
                sb.AppendLine($"> 配置项 {set.Key}");
                sb.AppendLine($"> 关键词:");
                set.Value.Keywords.ForEach(x=>sb.AppendLine(x));
                sb.AppendLine($"> 回复语句");
                set.Value.Replies.ForEach(x=>sb.AppendLine(x));
                sb.AppendLine($"> 奖励列表");
                set.Value.Rewards.ToList().ForEach(x=>sb.AppendLine($"{x.Key}:{x.Value.Min}-{x.Value.Max}"));
            }
            _amiable.SendMessage(sb.ToString());
        }

        private void ReloadCommand(string[] newargs)
        {
            AmiCheckin.LoadConfig();
            _amiable.SendMessage("> 重载配置成功");
        }
        

        public AmiableEventType EventType => AmiableEventType.Group;
    }
}